package com.sunwayworld.common.hibernate;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.metadata.ClassMetadata;

import com.sunwayworld.common.utils.SplitPage;
import com.sunwayworld.common.utils.StringUtils;
import com.sunwayworld.common.utils.VCommList;

public class HibernateAssert {

	public static Criteria copyCriteria(Class _class, Map map, Criteria criteria) {
		if (map == null)
			return criteria;
		Method[] methods = _class.getMethods();

		for (int i = 0; i < methods.length; i++) {
			String methodName = methods[i].getName();
			if ("get".equals(methodName.substring(0, 3))) {
				String _name = methodName.substring(3, methodName.length());
				_name = StringUtils.tofirstLowerCase(_name, 1);

				if (map.containsKey(_name)) {
					String _value = StringUtils.toString(map.get(_name));
					System.out.println("==value==" + _value);
					if (!"".equals(_value)) {
						// Class _name_class = methods[i].getClass();
						// Object val = BeanUtils.infoEncode(_value,
						// _name_class);
						Criterion t = Expression
								.like(_name, "%" + _value + "%");
						criteria.add(t);
					}
				}

			}

		}
		return criteria;
	}

	/**
	 * 
	 * @param vlist
	 *            取回的list
	 * @param spage
	 * @param nCount
	 *            总条数
	 */
	@SuppressWarnings("unchecked")
	public static VCommList fillCommList(List list, SplitPage spage, int nCount) {

		if (list == null) {
			return null;
		}
		VCommList vlist = new VCommList();
		vlist.addAll(list);
		if (spage == null) {
			spage = new SplitPage();
			spage.setNumPerPage(SplitPage.CONST_NUMPERPAGE);
			spage.setCurrPage(SplitPage.CONST_CURRPAGE);
		}
		vlist.setNumPerPage(spage.getNumPerPage());
		vlist.setCurrPage(spage.getCurrPage());
		vlist.setNum(nCount);
		vlist.setPageNum(nCount / vlist.getNumPerPage());
		if ((vlist.getPageNum() * vlist.getNumPerPage()) < nCount)
			vlist.setPageNum(vlist.getPageNum() + 1);

		return vlist;
	}

	public static void fillValueObject(Object obj, ClassMetadata classMeta) {
		if (null == obj || null == classMeta) {
			return;
		}
		String[] names = classMeta.getPropertyNames();
		boolean[] nullable = classMeta.getPropertyNullability();

		for (int i = 0, n = names.length; i < n; i++) {
			Object value = classMeta.getPropertyValue(obj, names[i],
					EntityMode.POJO);
			if (!nullable[i] && null == value) {
				try {
					Class parClass = obj.getClass().getMethod(
							"get" + StringUtils.tofirstUpperCase(names[i], 1))
							.getReturnType();
					String par = parClass.getName();
					
					Method tMethod = obj.getClass().getMethod(
							"set" + StringUtils.tofirstUpperCase(names[i], 1),
							parClass);

					if (par.equals("java.lang.String")) {
						String tmpValue = "1";
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.lang.Integer")) {
						Integer tmpValue = new Integer(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.lang.Long")) {
						Long tmpValue = new Long(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.sql.Date")) {
						Date tmpValue = new java.util.Date();
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.util.Date")) {
						Date tmpValue = new java.util.Date();
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}

					if (par.equals("java.sql.Timestamp")) {
						Timestamp tmpValue = new Timestamp(new Date().getTime());
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.lang.Double")) {
						Double tmpValue = new Double(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("java.lang.Float")) {
						Float tmpValue = new Float(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("double")) {
						Double tmpValue = new Double(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("int")) {
						Integer tmpValue = new Integer(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
					if (par.equals("float")) {
						Float tmpValue = new Float(-1);
						Object[] tmp = { tmpValue };
						tMethod.invoke(obj, tmp);
					}
				} catch (InvocationTargetException ex) {
				} catch (IllegalArgumentException ex) {
				} catch (IllegalAccessException ex) {
				} catch (SecurityException e) {
				} catch (NoSuchMethodException e) {
				}
			}
		}

	}
}
